package com.example.motioneventes;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class MyViewGroup extends LinearLayout {
    private static final String TAG = "MyViewGroup";
    private float mDownX;
    private float mDownY;

    public MyViewGroup(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Log.d(TAG, "dispatchTouchEvent: "+ev.getAction());
        //super.dispatchTouchEvent(ev  分发事件
        //true 事件被消费
        //false  返回给父容器 activity TouchEvent
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        Log.d(TAG, "onInterceptTouchEvent: "+ev.getAction());
        //super.onInterceptTouchEvent(ev);  事件分发
        //true  事件拦截  事件给自己的TouchEvent
        // flase 返回和super.onInterceotTouchEvent 一样

        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mDownX = ev.getX();
                mDownY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float x = ev.getX();
                float y = ev.getY();
                float dx = x - mDownX;
                float dy = y - mDownY;
                if (Math.abs(dx)<Math.abs(dy)){
                    //垂直
                    return true;
                }
                break;

        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "onTouchEvent: "+event.getAction());
        return true;
    }
}
